handle the case of \r and \n split across lines. Bug #337022. add unit
authorPaolo Borelli <pborelli@katamail.com>
Wed, 5 Apr 2006 18:30:46 +0000 (18:30 +0000)
committerPaolo Borelli <pborelli@src.gnome.org>
Wed, 5 Apr 2006 18:30:46 +0000 (18:30 +0000)
2006-04-05  Paolo Borelli  <pborelli@katamail.com>

* gtk/gtktextiter.c (gtk_text_iter_ends_line): handle the case of \r
and \n split across lines. Bug #337022.
* tests/testtextbuffer.c: add unit test.

ChangeLog
ChangeLog.pre-2-10
gtk/gtktextiter.c
tests/testtextbuffer.c

index 45862ac2a4ff044fc3df44a15bd2d0a4c6569f18..651a9defeb01f93a78f2b7553270d98f6fd1ec9c 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,9 @@
+2006-04-05  Paolo Borelli  <pborelli@katamail.com>
+
+       * gtk/gtktextiter.c (gtk_text_iter_ends_line): handle the case of \r
+       and \n split across lines. Bug #337022.
+       * tests/testtextbuffer.c: add unit test.
+
 Tue Apr  4 12:25:36 2006  Søren Sandmann  <sandmann@redhat.com>
 
        * gdk/x11/gdkimage-x11.c (gdk_image_class_init): Add "_private" to
index 45862ac2a4ff044fc3df44a15bd2d0a4c6569f18..651a9defeb01f93a78f2b7553270d98f6fd1ec9c 100644 (file)
@@ -1,3 +1,9 @@
+2006-04-05  Paolo Borelli  <pborelli@katamail.com>
+
+       * gtk/gtktextiter.c (gtk_text_iter_ends_line): handle the case of \r
+       and \n split across lines. Bug #337022.
+       * tests/testtextbuffer.c: add unit test.
+
 Tue Apr  4 12:25:36 2006  Søren Sandmann  <sandmann@redhat.com>
 
        * gdk/x11/gdkimage-x11.c (gdk_image_class_init): Add "_private" to
index f233e6a288add163213167680afacc6ab67bfff4..4ba37f9cf5ee66ce02cd49d7e0458e95369af6ec 100644 (file)
@@ -1564,10 +1564,23 @@ gtk_text_iter_ends_line (const GtkTextIter   *iter)
     return TRUE;
   else if (wc == '\n')
     {
+      GtkTextIter tmp = *iter;
+
       /* need to determine if a \r precedes the \n, in which case
-       * we aren't the end of the line
+       * we aren't the end of the line.
+       * Note however that if \r and \n are on different lines, they
+       * both are terminators. This for instance may happen after
+       * deleting some text:
+
+          1 some text\r    delete 'a'    1 some text\r
+          2 a\n            --------->    2 \n
+          3 ...                          3 ...
+
        */
-      GtkTextIter tmp = *iter;
+
+      if (gtk_text_iter_get_line_offset (&tmp) == 0)
+        return TRUE;
+
       if (!gtk_text_iter_backward_char (&tmp))
         return TRUE;
 
index 5da1cab0d643d54f9ae3f6622a66eeb5da3c3bbb..b739deadcc322dfb75f4c6fcf4cd23a10468851c 100644 (file)
@@ -960,6 +960,34 @@ test_line_separation (const char* str,
   g_object_unref (buffer);
 }
 
+/* there are cases where \r and \n should not be treated like \r\n,
+ * originally bug #337022. */
+static void
+split_r_n_separators_test (void)
+{
+  GtkTextBuffer *buffer;
+  GtkTextIter iter;
+
+  buffer = gtk_text_buffer_new (NULL);
+
+  gtk_text_buffer_set_text (buffer, "foo\ra\nbar\n", -1);
+
+  /* delete 'a' so that we have
+
+     1 foo\r
+     2 \n
+     3 bar\n
+
+   * and both \r and \n are line separators */
+
+  gtk_text_buffer_get_iter_at_offset (buffer, &iter, 5);
+  gtk_text_buffer_backspace (buffer, &iter, TRUE, TRUE);
+
+  g_assert (gtk_text_iter_ends_line (&iter));
+
+  gtk_text_buffer_get_iter_at_offset (buffer, &iter, 3);
+  g_assert (gtk_text_iter_ends_line (&iter));
+}
 
 static void
 line_separator_tests (void)
@@ -989,6 +1017,8 @@ line_separator_tests (void)
   test_line_separation (str, TRUE, FALSE, 2, 4, 5);
   g_free (str);
 
+  split_r_n_separators_test ();
+
   g_print ("Line separator tests passed\n");
 }